package cn.e4j.modular.crm.controller;

import cn.e4j.core.base.controller.BaseController;
import cn.e4j.core.common.exception.BizExceptionEnum;
import cn.e4j.core.dto.Dtos;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.dto.requestDtoResolver.DtoParam;
import cn.e4j.core.dto.requestDtoResolver.DtoWapper;
import cn.e4j.core.reqres.response.ErrorResponseData;
import cn.e4j.core.reqres.response.PageReData;
import cn.e4j.core.reqres.response.ResponseData;
import cn.e4j.core.reqres.response.SuccessResponseData;
import cn.e4j.core.shiro.ShiroKit;
import cn.e4j.core.shiro.ShiroUser;
import cn.e4j.modular.crm.entity.GwgfT;
import cn.e4j.modular.crm.service.GwgfTService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.time.LocalDate;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/crm/gwgi003")
public class Gwgi003Controller extends BaseController {
    @Autowired
    GwgfTService gwgfTService;

    /**
     * 跳转至大客户项目维护主界面
     *
     * @return
     */
    @RequiresPermissions("crm:gwgi003:init")
    @RequestMapping(value = {"", "/init"})
    public String index() {
        return "/pc/crm/gwgi003.html";
    }

    @RequiresPermissions("crm:gwgi003:init")
    @RequestMapping("/getHeadList")
    @ResponseBody
    public ResponseData getHeadList(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        Page<Object> page = dtoWapper.getPage();
        List<Map<String, Object>> list = gwgfTService.getHeadList(page, inDto);
        return new PageReData(page, list);
    }

    @RequiresPermissions("crm:gwgi003:add")
    @RequestMapping("/add")
    @ResponseBody
    public ResponseData add(GwgfT gwgfT) {
        if (gwgfTService.existedTheParams(gwgfT)) {
            return new ErrorResponseData(BizExceptionEnum.EXISTED_THE_PARAMS.getCode(),
                    BizExceptionEnum.EXISTED_THE_PARAMS.getMessage());
        } else {
            gwgfTService.add(gwgfT);
            return SUCCESS_TIP;
        }
    }

    @RequiresPermissions("crm:gwgi003:edit")
    @RequestMapping("/edit")
    @ResponseBody
    public ResponseData edit(GwgfT gwgfT) {
        gwgfTService.edit(gwgfT);
        return SUCCESS_TIP;
    }


    @RequiresPermissions("crm:gwgi003:del")
    @RequestMapping("/del")
    @ResponseBody
    public ResponseData delRow(GwgfT gwgfT) {
        gwgfTService.delete(gwgfT);
        return SUCCESS_TIP;
    }

    @RequiresPermissions("crm:gwgi003:del")
    @RequestMapping("/delBatch")
    @ResponseBody
    public ResponseData delRowsBatch(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<GwgfT> gwgfTList = inDto.getReqList(GwgfT.class);
        gwgfTService.deleteBatch(gwgfTList);
        return SUCCESS_TIP;
    }

    @RequiresPermissions("crm:gwgi003:add")
    @RequestMapping("/getAddData")
    @ResponseBody
    public ResponseData getAddData(@DtoParam DtoWapper dtoWapper) {
        Dto outDto = Dtos.newDto();
        ShiroUser shiroUser = ShiroKit.getUser();
        outDto.put("CURRENT_USER_ID",shiroUser.getID());
        outDto.put("CURRENT_USER_NAME",shiroUser.getNAME());
        outDto.put("CURRENT_ORG",shiroUser.getORG_ID());
        outDto.put("CURRENT_ORG_NAME",shiroUser.getSysOrgT().getNAME());
        outDto.put("CURRENT_DATE", LocalDate.now());
        return new SuccessResponseData(outDto);
    }


    @RequiresPermissions("crm:gwgi001:edit")
    @RequestMapping("/changeStatus")
    @ResponseBody
    public ResponseData changeStatus(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<GwgfT> gwgfTList = inDto.getReqList(GwgfT.class);
        gwgfTService.changeStatusBatch(gwgfTList);
        return SUCCESS_TIP;
    }

}
